<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\LoginRecord;
use App\Models\LotteryRecord;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends BaseAdminController
{
    //
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = new User();
    }

    /**
     * 会员列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
     */
    public function index(){
        if(\request()->ajax()){
            $page = \request()->input('page',1);
            $pageSize = \request()->input('pageSize',20);
            $search = \request()->input('search','');
            $status = \request()->input('status',-1);
            $where = [];
            if($search){
                $where[] = ['like'=>['account|nickname|email','%'.$search.'%']];
            }
            if($status == -1){
                $where[] = ['and'=>['status','>','-1']];
            }else{
                $where[] = ['and'=>['status','=',$status]];
            }
            $fields = ['id','account','nickname','email','sex','score','status'];
            $order = [
              'status'=>'desc',
              'score'=>'desc',
            ];
            $pageData = $this->model->getPages($page,$pageSize,$where,$fields,$order);
            /*if($pageData['data']['pages']){
            	foreach ($pageData['data']['pages'] as $value){
            		$value->score = round($value->score,2);
	            }
            }*/
            return response()->json(['status'=>$pageData['status'],'msg'=>$pageData['msg'],'page'=>$page,'totalCount'=>$pageData['data']['totalCount'],'pages'=>$pageData['data']['pages']]);
        }else{
            return view('admin.user.index');
        }
    }

    /**
     * 添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function add(){
        return view('admin.user.add');
    }

    public function addStore(){
        $input = \request()->all();
        $input['score'] = 0;
        $res = $this->model->add($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function edit(){
        return view('admin.user.edit');
    }

    public function editStore(){
        $input = \request()->all();
       if(isset($input['score'])){
           unset($input['score']);
       }
        if(isset($input['account'])){
            unset($input['account']);
        }
        $res = $this->model->edit($input);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(){
        $id = \request()->input('id',0);
        $res = $this->model->detail($id);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 状态修改
     * @return \Illuminate\Http\JsonResponse
     */
    public function status(){
        $id = \request()->input('id',0);
        $opt = \request()->input('opt','');
        $res = $this->model->changeStatus($id,$opt);
        return response()->json(Common::dataFormat($res['status'],$res['msg'],$res['data']));
    }

    /**
     * 用户登陆记录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
     */
    public function loginRecord(){
        if(\request()->ajax()){
            $page = \request()->input('page',1);
            $pageSize = \request()->input('pageSize',20);
            $id = \request()->input('id',0);
            $where = [
                ['and'=>['user_id','=',$id]],
                ['and'=>['type','=',1]]
            ];
            $order = [
                'login_time'=>'desc'
            ];
            $recordModel = new LoginRecord();
            $pageData = $recordModel->getPages($page,$pageSize,$where,'*',$order,'user');
            if($pageData['data']['pages']){
                foreach ($pageData['data']['pages'] as $value){
                    $value->user_account = '';
                    if($value->user){
                        $value->user_account = $value->user->account;
                    }
                    unset($value->user);
                    $value->login_time = date('Y-m-d H:i:s',$value->login_time);
                }
            }
            return response()->json(['status'=>$pageData['status'],'msg'=>$pageData['msg'],'page'=>$page,'totalCount'=>$pageData['data']['totalCount'],'pages'=>$pageData['data']['pages']]);

        }else{
            return view('admin.user.record');
        }
    }

	/**
	 * 下拉框列表
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function selectList(){
    	$lists = $this->model->getAllList([['and'=>['status','=',1]]],['id','nickname'],['score'=>'desc']);
    	return response()->json(Common::dataFormat(config('statusInfo.status.success'),config('statusInfo.status.success'),$lists));
    }
}
